<?php

namespace App\Http\Controllers\Notify;

use App\Enums\OrderEnum;
use App\Http\Controllers\BaseController;
use App\Http\Logic\OrderLogic;
use App\Http\Logic\PayLogic;
use App\Http\Requests\TyNotifyRequest;
use App\Jobs\NoticeUser;
use App\Logic\LogLogic;
use App\Logic\MachineLogic;
use App\Models\SwtOrder;
use App\Models\SwtTyGood;

class TyNotifyController extends BaseController
{
    public function tyResponse($message = "Success", $code = 0)
    {
        $res = [
            'code'      => $code,
            'message'   => $message
        ];

        return response()->json($res);
    }

    /**
     * @notes 拓元商品模型审核结果回调
     * @param TyNotifyRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @author Evil丿
     * @date 2024/12/05 17:35
     */
    public function applyGoodsResult(TyNotifyRequest $request)
    {
        $params = $request -> all();

        LogLogic::setTyLogs($params['acceptanceId'], 'apply_goods_notify',$params);

        $info = SwtTyGood::firstWhere('acceptance_id',$params['acceptanceId']);

        $info -> is_register = $params['status'];

        $info -> desc = $params['message'];

        $res = $info -> save();

        if(!$res) {
            return $this->tyResponse('商品信息修改失败', 500);
        }

        return $this->tyResponse();
    }

    /**
     * @notes 拓元设备注册回调
     * @param TyNotifyRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     * @author Evil丿
     * @date 2024/06/14 15:32
     */
    public function setMachineInfoResult(TyNotifyRequest $request)
    {
        $params = $request -> all();

        LogLogic::setTyLogs($params['cupId'], 'register_machine_notify',$params);

        $cupId = str_replace('SWT','', $params['cupId']);

        $res = MachineLogic::updateTuoYuanMachineRegister($cupId, $params['status']);

        if(!$res) {
            return $this->tyResponse('设备信息修改失败', 500);
        }

        return $this->tyResponse();
    }

    /**
     * @notes 拓元视频识别结果回调
     * @param TyNotifyRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @author Evil丿
     * @date 2024/12/12 15:20
     */
    public function recognitionVideos(TyNotifyRequest $request)
    {
        $params = $request->all();

        LogLogic::setTyLogs($params['taskId'], 'recognition_video_notify', $params);

        $orderId = str_replace('SWT','', $params['orderId']);

        $info = SwtOrder::firstWhere('order_sn', $orderId);

        if(!$info) {
            return $this->tyResponse('订单未找到', 404);
        }

        if($info->state !== OrderEnum::ORDER_DOING) {
            return $this->tyResponse();
        }

        $result = $params['result'];

        if($result['status'] !== 20000) {
            OrderLogic::setWarningOrder($info->order_sn, $info->door_num, $result['status'].'-'.$result['errmsg']);

            NoticeUser::dispatch($info -> id, 3, $result['status'].'-'.$result['errmsg'])->onQueue('notice_user');

            return $this->tyResponse();
        }

        $goods = $result['result'];

        $amount = 0;

        $order_goods = [];

        foreach($goods as $good) {
            $price = (int) bcmul($good['price'],100);

            $amount += $price * $good['number'];

            $order_goods[] = [
                "barcode"  => $good['skuId'],
                "name"     => $good['skuName'],
                "quantity" => $good['number'],
                "amount"   => $price * $good['number']
            ];
        }

        $res = PayLogic::goPay($info -> toArray(), $amount, $order_goods);

        if($res === false) {
            return $this->tyResponse(PayLogic::getErrorMessage(), -1);
        }

        $info -> video_status = 2;

        $info -> save();

        return $this->tyResponse();
    }
}
